package com.zcf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.common.result.ResultVo;
import com.zcf.pojo.Address;
import com.zcf.service.AddressService;
import com.zcf.vo.in.PageVo;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/address")
@CrossOrigin
@ResponseBody
public class AddressController {

	@Autowired
	private AddressService addressService;

	// 添加地址
	@PostMapping("/addAddress")
	public Body addAddress(String userId, String address, String phone, String name, String detail) {
		return addressService.addAddress(userId, address, phone, name, detail);
	}

	// 修改地址
	@PostMapping("/upAddress")
	public Body upAddress(String id, String userId, String address, String phone, String name, String detail) {
		return addressService.upAddress(id, userId, address, phone, name, detail);
	}

	// 删除地址
	@PostMapping("/deletAddress")
	public Body deletAddress(String id) {
		Address address = new Address();
		address.setId(id);
		boolean b = address.deleteById();
		if (b) {
			return Body.newInstance(200, "删除成功");
		}
		return Body.newInstance(501, "删除失败");
	}

	// 设置默认
	@PostMapping("/setMoren")
	public Body setMoren(String userId, String id) {
		return addressService.setMoren(userId, id);
	}

	/**
	 * 查看用户默认地址
	 */
	@PostMapping("queryMoren")
	public Body queryMoren(String userId) {
		Address address = new Address();
		EntityWrapper<Address> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.eq("type", 1);
		address = address.selectOne(ew);
		if (address != null) {
			return Body.newInstance(address);
		}
		return Body.newInstance(-1, "没有设置默认地址");
	}

	// 地址列表
	@PostMapping("/addressList")
	public Body addressList(PageVo pv, String userId) {
		ResultVo rv = addressService.addressList(pv, userId);
		return Body.newInstance(rv);
	}
	
	public Body ceshi() {
		
		return Body.newInstance();
	}
}
